package com.example.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.exception.CustomException;
import com.example.mapper.MetaMapper;
import com.example.pojo.utils.RouteMeta;
import com.example.service.MetaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TODO
 *
 * @author ding
 * @since 2024/7/2
 */
@Service
@DS("master")
public class MetaSetviceImp extends ServiceImpl<MetaMapper, RouteMeta> implements MetaService {

    @Autowired
    private MetaMapper metaMapper;

    /**
     * 新增菜单时，新增的菜单样式相关数据
     */
    public void addMeta(RouteMeta routeMeta) {
        List<RouteMeta> routeMetaList = metaMapper.selectList(new LambdaQueryWrapper<RouteMeta>().eq(RouteMeta::getMenuId, routeMeta.getMenuId()));
        if (ObjectUtil.isNotEmpty(routeMetaList) && routeMetaList.size() > 0) {
            throw new CustomException(ResultCodeEnum.META_INSERT_ERROR);
        }
        //如果样式对应的菜单没有的菜单样式表单，则可以添加新的样式表单
        metaMapper.insert(routeMeta);
    }
}
